var utils = require("../BleMash-js/util.js");
var Ajax = require("../../utils/ajax.js")
var Modal = require("../../utils/modal.js")
var app = getApp();
Page({
  data: {
    showColorPicker: false,
    index: 0,
    warm: 0,
    cold: 0,
    colorData: {
      //基础色相，即左侧色盘右上顶点的颜色，由右侧的色相条控制
      hueData: {
        colorStopRed: 255,
        colorStopGreen: 0,
        colorStopBlue: 0,
      },
      //选择点的信息（左侧色盘上的小圆点，即你选择的颜色）
      pickerData: {
        x: 0, //选择点x轴偏移量
        y: 480, //选择点y轴偏移量
        red: 0,
        green: 0,
        blue: 0,
        hex: '#000000'
      },
      //色相控制条的位置
      barY: 0
    },
    rpxRatio: 1 //此值为你的屏幕CSS像素宽度/750，单位rpx实际像素
  },
  onLoad(e) {
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 3]; //上上个页面
    this.setData({
      lights: prevPage.data.lightsin,
      scene: app.globalData.activescene,
      index: e.index,
      phoneheight: app.globalData.phoneheight,
      fix_position: app.globalData.fix_position,
      phonewidth: app.globalData.phonewidth,
      rpxRatio: app.globalData.phonewidth / 750
    });
    Ajax.AsyncPost("/group/get_scene_lights/", { sceneid: app.globalData.activescene.id }, (data) => {
      var scene_lights = data.scene_lights;
      this.setData({
        lights: scene_lights
      });
    });
  },
  onChangeColor(e) {
    this.setData({
      colorData: e.detail.colorData
    });
  },
  change(e) {
    var x = this.data.control_index;
    if (x == 1000) {
      this.edit_scene();
    } else {
      this.addressControl();
    }
  },
  warmchange(e) {
    this.setData({ warm: e.detail.value });
  },
  coldchange(e) {
    this.setData({ cold: e.detail.value });
  },
  edit_scene() {
    Ajax.AsyncPost("/group/edit_scene/", {
      groupid: app.globalData.activegroup.id,
      sceneid: app.globalData.activescene.id,
      cold: this.data.cold,
      warm: this.data.warm,
      red: this.data.colorData.pickerData.red,
      green: this.data.colorData.pickerData.green,
      blue: this.data.colorData.pickerData.blue,
    }, (res) => {
      var scene = this.data.scene;
      scene.warm = this.data.warm;
      scene.cold = this.data.cold;
      scene.red = this.data.colorData.pickerData.red;
      scene.green = this.data.colorData.pickerData.green;
      scene.blue = this.data.colorData.pickerData.blue;
      this.setData({scene: scene});
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 3]; //上上个页面
      var lights = prevPage.data.lightsin
      for (let i = 0; i < lights.length; i++) {
        lights[i].warm = this.data.warm;
        lights[i].cold = this.data.cold;
        lights[i].red = this.data.colorData.pickerData.red;
        lights[i].green = this.data.colorData.pickerData.green;
        lights[i].blue = this.data.colorData.pickerData.blue;
      }
      this.setData({lights: lights });
      this.setData({ showColorPicker: false });
    });
    
  },
  addressControl() {
    var index = this.data.control_index;
    var lights = this.data.lights;
    var data = {
      lightid: lights[index].lightid,
      sceneid: app.globalData.activescene.id,
      groupid: app.globalData.activegroup.id,
      cold: lights[index].cold,
      warm: lights[index].warm,
      red: this.data.colorData.pickerData.red,
      green: this.data.colorData.pickerData.green,
      blue: this.data.colorData.pickerData.blue,
    };
    Ajax.AsyncPost("/group/edit_light_in_scene/", data, () => {
      lights[index].warm = lights[index].warm;
      lights[index].cold = lights[index].cold;
      lights[index].red = this.data.colorData.pickerData.red;
      lights[index].green = this.data.colorData.pickerData.green;
      lights[index].blue = this.data.colorData.pickerData.blue;
      this.setData({lights: lights});
      this.setData({ showColorPicker: false });
    });
  },
  showcolorpicker(e) {
    if (this.data.showColorPicker) {
      this.setData({showColorPicker: false});
    } else {
      this.setData({showColorPicker: true});
      var index = e.currentTarget.dataset.index;
      this.data.control_index = index;
    }
  },
  
})
